package Servlet;

import com.cyy.bean.cart;
import com.cyy.bean.collect;
import com.cyy.bean.history;
import com.cyy.dao.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet({"/addToCart","/addToHistory","/addToCollect"})
public class addServelt extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String productName=request.getParameter("productName");
        String userName=request.getSession().getAttribute("name").toString();
        int price=Integer.parseInt(request.getParameter("price"));
        productDao productDao = new productDao();
        int usability= 0;
        try {
            usability = productDao.selectUsabilityByName(productName);
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        }
        String newUrl="";
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String dateTime = df.format(date);
        String uri=request.getRequestURI();
        int recordId=0;
        if(uri.equals("/addToCart"))
        {
            cartDao cartDao=new cartDao();
            try {
//                查找购物车中是否已经出现该商品
                recordId = cartDao.selectByProductAndUser(userName, productName);
            } catch (SQLException | ClassNotFoundException throwables) {
                throwables.printStackTrace();
            }
            newUrl="/showCart";
//            已经出现，更新对应的数量
            if (recordId > 0) {
                try {
                    recordDao.updateNum(recordId);
                } catch (SQLException | ClassNotFoundException throwables) {
                    throwables.printStackTrace();
                }
//                未出现，插入新的数据
            } else {
                try {
                    cartDao.add(new cart(0,userName,productName,price,1,dateTime,1));
                } catch (SQLException | ClassNotFoundException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        else if(uri.equals("/addToCollect"))
        {
            collectDao collectDao=new collectDao();
            try {
                recordId = collectDao.selectByProductAndUser(userName, productName);
            } catch (SQLException | ClassNotFoundException throwables) {
                throwables.printStackTrace();
            }
            newUrl="/showCollect";
            String message;
            if(recordId>0)
            {
                message="该物品已经收藏过！";
                System.out.println(message);
            }
            else
            {
                message="添加成功！";
                try {
                    collectDao.add(new collect(0,userName,productName,price,1,dateTime,1));
                } catch (SQLException | ClassNotFoundException throwables) {
                    throwables.printStackTrace();
                }
            }
            request.setAttribute("message",message);
        }
        else
        {
            newUrl="/showHistory";
            if(!request.getSession().getAttribute("address").toString().equals("未填")&&
                    !request.getSession().getAttribute("address").toString().equals("未填")){
                historyDao historyDao=new historyDao();
                try {
                    historyDao.add(new history(0,userName,productName,price,1,dateTime,0,usability));
                } catch (SQLException | ClassNotFoundException throwables) {
                    throwables.printStackTrace();
                }
            }
            else
            {
                request.setAttribute("message","收货地址或联系方式不能为空！请到个人信息中修改！");
            }
        }
        request.getRequestDispatcher(newUrl).forward(request,response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
